﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Repository;
using GoodMan.Infrastructure.Entities.Enum;
using GoodMan.Infrastructure.Entities.Cache;

namespace GoodMan.Infrastructure.Entities.Entity
{
    public class User : IAccessible<T_User,User>, ILogicEntity<T_User,User>
    {
        #region
        public string UserName { get; set; }
        public string Password { get; set; }
        public string NickName { get; set; }
        public string RealName { get; set; }
        public Gender Gender { get; set; }
        public DateTime Birthday { get; set; }
        public string QQ { get; set; }
        public string Phone { get; set; }
        public string PhoneOne { get; set; }
        public string Photo { get; set; }
        public DateTime CreateDate { get; set; }
        public bool IsRefuse { get; set; }
        public bool IsActivate { get; set; }
        public string Token { get; set; }
        public UserRole SystemRole { get; set; }
        public int CollegeId { get; set; }
        /// <summary>
        /// 楼栋号
        /// </summary>
        public int FloorId { get; set; }
        public string Dormitory { get; set; }
        public string IdCardNo { get; set; }
        public string RebateLevel { get; set; }

        /// <summary>
        /// 院系
        /// </summary>
        public string Departments { get; set; }
        /// <summary>
        /// 班级
        /// </summary>
        public string Classes { get; set; }
        #endregion

        public Dormitory dormitory { get; set; }

        public User LoadDormitory(T_Dormitory dormitory)
        {
            this.dormitory = new Dormitory().Set(dormitory);
            return this;
        }

        //以下这段是不需要的
        public List<Dormitory> dormitories { get; set; }

        public User LoadDormitories(List<Dormitory> dormitories)
        {
            this.dormitories = dormitories;
            return this;
        }
        //End.................................


        protected override void Covariant(T_User model)
        {
            this.UserName = model.UserName;
            this.Password = model.Password;
            this.NickName = model.NickName;
            this.RealName = model.RealName;
            this.Gender = model.Gender ? Enum.Gender.Male : Enum.Gender.Female;
            this.Birthday = model.Birthday;
            this.QQ = model.QQ;
            this.Phone = model.Phone;
            this.Photo = model.Photo;
            this.CreateDate = model.CreateDate;
            this.IsRefuse = model.IsRefuse;
            this.IsActivate = model.IsActivate;
            this.Token = model.Token;
            this.SystemRole = (UserRole)model.SystemRole;
            this.CollegeId = model.CollegeId;
            this.FloorId = model.FloorId;
            this.Dormitory = model.Dormitory;
            this.IdCardNo = model.IdCardNo;
            this.RebateLevel = model.RebateLevel;
            this.PhoneOne = model.PhoneOne;
            this.Departments = model.Departments;
            this.Classes = model.Classes;
        }
        protected override T_User Inverter(User model)
        {
            return new T_User()
            {
                Birthday = model.Birthday,
                Gender = model.Gender == Enum.Gender.Male,
                IsActivate = model.IsActivate,
                IsRefuse = model.IsRefuse,
                NickName = model.NickName,
                Password = model.Password,
                Phone = model.Phone,
                Photo = model.Photo,
                QQ = model.QQ,
                Token = model.Token,
                UserName = model.UserName,
                SystemRole = (int)model.SystemRole,
                CollegeId = model.CollegeId,
                CreateDate = model.CreateDate,
                Dormitory = model.Dormitory,
                FloorId = model.FloorId,
                IdCardNo = model.IdCardNo,
                RealName = model.RealName,
                RebateLevel = model.RebateLevel,
                PhoneOne = model.PhoneOne,
                Departments = model.Departments,
                Classes = model.Classes
            };
        }
        public User Set(T_User dbModel)
        {
            Covariant(dbModel);
            return this;
        }

        /// <summary>
        /// 将启用缓存,通过username查找
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public override User Find(string key)
        {
            return new UserCache().GetItem(key);
        }
        public override User PostDelete()
        {
            new UserCache().RefreshItem(UserName);
            return base.PostDelete();
        }
        public override User PostModify()
        {
            new UserCache().RefreshItem(UserName);
            return base.PostModify();
        }
    }
}
